package Java学习.集合;

import java.util.ArrayList;
import java.util.Scanner;

public class 添加用户判断存在 {
    public static void main(String[] args) {
        //定义一个集合
        ArrayList<User> list=new ArrayList<>();
        //创建3个用户对象
        User u1=new User(1,"张三","12345");
        User u2=new User(2,"李四","123456");
        User u3=new User(3,"王五","1234567");
        //添加对象到集合中
        list.add(u1);
        list.add(u2);
        list.add(u3);
        //用户输入
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入你要查询的id:");
        int id=sc.nextInt();
        System.out.println(find(list,id));
    }
    //定义一个方法根据id查找用户id
    public static int find(ArrayList<User> u, int id){
        for (int i = 0; i < u.size(); i++) {
            if (u.get(i).getId()==id){
                return i;
            }
        }
        return -1;
    }
}
